<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 公共
 * @author www.dingwei.cn
 */
class Common extends MY_Controller {
	
	/**
	 * @var AdmSettingModel
	 */
	public $mSett = null;
	
	/**
	 * @var DlnCaptchaModel
	 */
	public $mCaptcha = null;
	
	/**
	 * 获取银行列表
	 */
    public function getBanks(){
    	
    	$this->loadModel('AdmSettingModel', 'mSett', 'admin');
    	$data = $this->mSett->getBanks();
    	$this->appReturn(1, '', $data);
    }
    
    /**
     * 获取客户电话
     */
    public function getCustomerTel(){
   	
    	$this->loadModel('AdmSettingModel', 'mSett', 'admin');
    	$tS = $this->mSett->get('customer_tel', 'deliveryman');
    	$this->appReturn(1, '', ['tel' => $tS]);
    }
    
    /**
     * 关于我们接口
     */
    public function aboutBs(){
   	
    	$this->loadModel('AdmSettingModel', 'mSett', 'admin');
    	$tS = $this->mSett->getExplain('about_us');
    	$this->appReturn(1, '', $tS);
    }
    
    /**
     * 提现说明
     */
    public function withdraw(){
   	
    	$this->loadModel('AdmSettingModel', 'mSett', 'admin');
    	$tS = $this->mSett->get('withdraw_cash_description', 'deliveryman');
    	$this->appReturn(1, '', $tS);
    }
    
    /**
     * 预计收益说明
     */
    public function income(){
        
        $this->loadModel('AdmSettingModel', 'mSett', 'admin');
        $tS = $this->mSett->get('income_description', 'deliveryman');
        $this->appReturn(1, '', $tS);
    }
    
    public function getArea(){
        $this->loadModel('AdmSettingModel', 'mSett', 'admin');
        
        //表单验证
        $vConf = array(
                    array('pid', '上级id', 'required|is_natural'),
        );
        $this->formDataValidation($vConf);
        $data = $this->mSett->getLocationByPid(post('pid'));
        $this->appReturn(1, '', $data);
    }
    
    /**
     * 版本更新
     * type = 1 安卓
     * type = 2 ios
     */
    public function update(){
        
        $this->loadModel('AdmSettingModel', 'mSett', 'admin');
        //表单验证
        $vConf = array(
            array('type', 'app类型', 'required|in_list[1,2]'),
        );

        $type = get('type');
        $this->formDataValidation($vConf,array('type' => $type));
        if($type == 1){
            $tA = $this->mSett->get('app_android_update' ,'deliveryman');
            $data = json_decode($tA, true);

        }else{
            $tA = $this->mSett->get('app_ios_update','deliveryman');
            $data = json_decode($tA, true);
        }

        
        $this->appReturn(1, '', $data);
    }
    
    /**
     * 注册协议
     */
    public function agreement(){
        
        $this->loadModel('AdmSettingModel', 'mSett', 'admin');
        $tS = $this->mSett->get('agreement', 'deliveryman');
        
        $this->appReturn(1, '', $tS);
    }
    
    /**
     * 获取验证码
     */
    public function captcha(){
        
        $this->loadModel('DlnCaptchaModel', 'mCaptcha');
        
        $this->load->helper('captcha');
        
        $path = 'uploads/captcha/';
        if( ! is_dir($path))
        {
            mkdir($path, 0777, TRUE);
        }	
        
        $vals = [
            'word'      => '',
            'img_path'  => $path,
            'img_url' => base_url('uploads/captcha'),
            'font_path' => 'static/font/Torcing.ttf',
            'img_width' => 150,
            'img_height'    => 75,
            'expiration'    => 7200,
            'word_length'   => 4,
            'font_size' => 25,
            'img_id'    => 'Imageid',
            'pool'      => '0123456789',
            
            // White background and border, black text and red grid
            'colors'    => array(
                'background' => array(255, 255, 255),
                'border' => array(255, 255, 255),
                'text' => array(0, 0, 0),
                'grid' => array(255, 40, 40)
            )
        ];
        
        $cap = create_captcha($vals);

        if($cap){
            
            $data = [
                        'key' => uniqid().rand(100, 1000).rand(1, 10000),
                        'code' => $cap['word'],
                        'addtime' => time()
            ];
            
            if($this->mCaptcha->insert($this->mCaptcha->tbCaptcha, $data)){
                $this->db->where('addtime <=', time() - 30 * 60);
                $this->mCaptcha->delete($this->mCaptcha->tbCaptcha);
                $data = [
                         'key' => $data['key'],
                         'image' => trim($vals['img_url'], '/').'/'.$cap['filename']
                ];
                $this->appReturn(1, '', $data);
            }
        }
        
        $this->appReturn(0, '系统繁忙');
    }

    /**
     * 获取分享参数
     */
    public function getSharp(){
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
        $tA = $this->mSetting->get('sharp_title,sharp_icon,sharp_url,sharp_desc', 'deliveryman');
        $tA['sharp_icon'] = getImage($tA['sharp_icon'],'og');
        $this->appReturn(1,'',$tA);
    }

    /**
     * 分享下载页面
     */
    public function share(){
        $this->loadModel('AdmSettingModel', 'mSetting', 'admin');
        $tA = $this->mSetting->get('android_update_url,ios_update_url,', 'deliveryman');
        $this->view('share/share',[
                'android'=>$tA['android_update_url'],
                'ios'=>$tA['ios_update_url'],
            ]
        );
    }

}